/*
 * main.c
 *
 *  Created on: 2011/04/23
 *      Author: AKS
 */

#include "core/include/types.h"
#include "core/include/aks_processor.h"
#include "core/include/process.h"

#include "app/utility/include/utility.h"
#include "app/memory/include/memory.h"

int main(void)
{
	// initialize hardware
	// -- initialize main processor
	if( TRUE != Init_processor(32) )
	{
		while(1);
	}

	// -- initialize peripherals
    if( TRUE == Init_UART(UART_DBG,        57600) )
    {
        UART_transmitString(UART_DBG, (BYTE *)"Success to initialize UART(Debug Port)\r\n");
    }


//  if( TRUE != Init_UART(UART_SUB_SYSTEM, 57600) ) while(1);
    if( TRUE == Init_Timer(1) )
    {
        UART_transmitString(UART_DBG, (BYTE *)"Success to initialize TIMER(Tick = 1ms)\r\n");
    }
    if( TRUE == Init_I2C(0x01, 100))
    {
    	UART_transmitString(UART_DBG, (BYTE *)"Success to initialize I2C Bus\r\n");
    }
//  if( TRUE != Init_SPI(16000) )       while(1);
    if( TRUE == Init_GPIO() )
    {
        UART_transmitString(UART_DBG, (BYTE *)"Success to initialize GPIO\r\n");
    }
//  if( TRUE != Init_SubSystem() )      while(1);

    UART_transmitString(UART_DBG, (BYTE *)"End of power on hardware reset..\r\n");

	// initialize software core
    initProcessController();

    UART_transmitString(UART_DBG, (BYTE *)"End of Process Scheduler Initialize..\r\n");

    Enable_Int();

    // add the boot up task to run system.
    entryNewProcess(&initialProcessItm);

	// check process permission
	while(1)
	{
		/// 1. check wake up condition( for each waiting process )
        /// 2. wake up next process
		/// 3. execute process with parameter
		/// Note. the other implementation, checkWakeUp() can be call in timer handler
		if(0 < checkWakeUp() && TRUE == wakeUpNextProcess() )
		{
			((PROCESS*)executingProcess->load)->function(((PROCESS*)executingProcess->load)->param);
		}
		else
		{
			;
		}
	}

	return 0;
}
